<template>
	<view class="columnbox wdh-100 content">
		<view class="columnbox wdh-100 search-box">
			<view class="rowbox wdh-100 spb">
				<view class="rowbox search-info spb">
					<view class="rowbox">
						<u-icon name="search" size="40rpx"></u-icon>
						<input @input="searchInput" v-model="name" placeholder="请输入搜索内容" />
					</view>
					<view class="rowbox search-btn" @click="searchInfo">搜索</view>
				</view>
				<view class="rowbox search-btn add" @click="addChufang">+新建处方</view>
			</view>
			<scroll-view scroll-x class="wdh-100 scroll-list">
				<view @click="setType(index)" :class="{'sel-type':curtype==index}" class="one-type"
					v-for="(item,index) in typeList" :key='index'>{{item.name}}</view>
			</scroll-view>
			<scroll-view scroll-x class="wdh-100 scroll-list">
				<view @click="setJixing(index)" :class="{'sel-type':curjixing==index}" class="one-type"
					v-for="(item,index) in jixingList" :key='index'>{{item.name}}</view>
			</scroll-view>
			<scroll-view scroll-x class="wdh-100 scroll-list">
				<view @click="setClassify(item.value)" :class="{'sel-type':curclassify==item.value}" class="one-type"
					v-for="(item,index) in classify" :key='index'>{{item.name}}</view>
			</scroll-view>
		</view>
		<view style="width: 100%;height: 380rpx;"></view>
		<view class="columnbox one-info" v-for="(item,index) in yaofangList" :key='index'>
			<view class="rowbox wdh-100 spb">
				<view class="yf-name">{{item.name}}</view>
				<view class="rowbox yaofangtype">{{item.yaofangtype}}</view>
			</view>
			<view class="wdh-100 cf-list">
				<text v-for="(cf,cfindex) in item.cfList" :key='cfindex'>{{cf}}，</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				typeList: [], //分类
				curtype: 0,
				jixingList: [], //剂型
				curjixing: 0,
				classify: [{
						name: '系统处方',
						value: 1
					},
					{
						name: '我的处方',
						value: 2
					}
				],
				curclassify: 1,
				pageNo: 1,
				total: 0,
				yaofangList: [],
				name: '',
				searchflag: false,
				
			};
		},
		onShow() {
			this.curclassify = 1
		},
		onLoad() {
			this.initInfo()
		},
		onReachBottom() {
			if (this.yaofangList.length < this.total) {
				this.pageNo++
				this.getYaofang()
			}
		},
		methods: {
			addChufang() {
				uni.navigateTo({
					url: '/packageA/myKaifang/addYaofang'
				})
			},
			searchInput() {
				this.searchflag = false
				this.pageNo = 1
				this.yaofangList = []
			},
			searchInfo() {
				this.searchflag = true
				this.pageNo = 1
				this.yaofangList = []
				this.getYaofang()
			},
			setClassify(v) {
				this.curclassify = v
				this.pageNo = 1
				this.yaofangList = []
				this.getYaofang()
			},
			setType(index) {
				this.curtype = index
				this.pageNo = 1
				this.yaofangList = []
				this.getYaofang()
			},
			setJixing(index) {
				this.curjixing = index
				this.pageNo = 1
				this.yaofangList = []
				this.getYaofang()
			},
			async initInfo() {
				await this.getType()
				await this.getJixing()
				this.getYaofang()
			},
			getYaofang() {
				this.$req.post('/doctor/yaofangs', {
					pageNo: this.pageNo,
					pageSize: 20,
					yaofangtypeid: this.typeList[this.curtype].id,
					jixingid: this.jixingList[this.curjixing].id,
					doctorid: uni.getStorageSync('uid'),
					name: this.searchflag ? this.name : '',
					moban: this.curclassify,
					jixingcxid: this.jixingList[this.curjixing].id,
				}).then(res => {
					this.total = res.data.data.totalCount
					res.data.data.data.forEach(resp => {
						let list = []
						resp.Yaofangdrug.forEach(v => {
							list.push(v.chinesedrug + ' ' + v.gram + 'g')
						})
						resp.cfList = list
					})
					this.yaofangList = this.yaofangList.concat(res.data.data.data)
				})
			},
			getJixing() {

				return new Promise(resolve => {
					this.$req.post('/doctor/jixings').then(res => {
						this.jixingList = res.data.data
						this.jixingList.unshift({
							id: 0,
							name: '全部'
						})
						resolve(true)
					})
				})
			},
			getType() {
				return new Promise(resolve => {
					this.$req.post('/doctor/yaofangtypes').then(res => {
						this.typeList = res.data.data
						this.typeList.unshift({
							id: 0,
							name: '全部'
						})
						resolve(true)
					})
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.content {
		min-height: 100vh;
		background: #F5F8FB;
		justify-content: flex-start;
		font-family: PingFang SC;
	}

	.search-box {
		position: fixed;
		top: var(--window-top);
		left: 0;
		padding: 32rpx 36rpx;
		background: #fff;
		z-index: 9;

		.search-info {
			width: 450rpx;
			height: 70rpx;
			border: #00b6b2 solid 1px;
			border-radius: 100rpx;
			padding-left: 20rpx;

			input {
				font-size: 26rpx;
				margin-left: 10rpx;
			}
		}

		.search-btn {
			width: 110rpx;
			height: 50rpx;
			background: linear-gradient(to right, #01a7a1, #5acbc1);
			border-radius: 100rpx;
			font-size: 26rpx;
			color: #fff;
			margin-right: 10rpx;


		}

		.add {
			width: fit-content;
			padding: 0 20rpx;
			border-radius: 10rpx;
			background: #01a7a1;
		}

		.scroll-list {
			white-space: nowrap;
			width: 100%;
		}

		.one-type {
			margin-top: 40rpx;
			display: inline-block;
			font-family: PingFang SC;
			font-weight: bold;
			font-size: 24rpx;
			color: #202020;
			margin-right: 26rpx;
		}

		.sel-type {
			color: #00b6b2;
		}
	}

	.one-info {
		width: 710rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 30rpx 32rpx;
		margin-bottom: 20rpx;

		.yf-name {
			font-family: PingFang SC;
			font-weight: bold;
			font-size: 24rpx;
			color: #0E0E0E;
		}

		.yaofangtype {
			color: #00b6b2;
			font-size: 22rpx;
			line-height: 40rpx;
			padding: 0 10rpx;
			border-radius: 4rpx;
			background: rgba(#7db5b1, .1);
			border: #00b6b2 solid 1px;
		}

		.cf-list {
			margin-top: 20rpx;
			font-family: PingFang SC;
			font-size: 24rpx;
			color: #7f9090;

			text {
				margin-right: 20rpx;
				line-height: 40rpx;
			}
		}
	}
</style>